﻿using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Data.Util;
using JNPF.Utils;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 工作日志
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2019-12-17 
    /// </summary>
    internal class WorkLogService : DataFactory, WorkLogIService
    {
        /// <summary>
        /// 列表(我发出的)
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <param name="userId">用户主键</param>
        /// <returns></returns>
        public async Task<PageResult<WorkLogEntity>> GetSendList(PageInput input, string userId)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<WorkLogEntity>().And(m => m.CreatorUserId == userId);
                //关键字（名称、编码）
                if (!string.IsNullOrEmpty(input.keyword))
                {
                    queryWhere = queryWhere.And(m => m.Title.Contains(input.keyword) || m.Description.Contains(input.keyword));
                }
                return await db.GetIQueryable<WorkLogEntity>().Where(queryWhere).OrderBy(t => t.CreatorTime).GetPageResultAsync(input);
            }
        }
        /// <summary>
        /// 列表（我收到的）
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <returns></returns>
        public async Task<PageResult<WorkLogEntity>> GetReceiveList(PageInput input, string userId)
        {
            using (var db = this.Basic)
            {
                var parameters = new List<DbParameter>();
                var dbSql = new StringBuilder();
                dbSql.Append(@"select * from Ext_WorkLog where F_Id in(select F_WorkLogId  from Ext_WorkLogShare where F_ShareUserId = @userId)");
                parameters.Add(DataHelper.CreateParameter("@userId", userId));
                //关键词（消息标题）
                if (!input.keyword.IsEmpty())
                {
                    dbSql.Append(" AND F_Title like @keyword");
                    parameters.Add(DataHelper.CreateParameter("@keyword", '%' + input.keyword + '%'));
                }
                input.sidx = "F_CreatorTime";
                return await PageHelper.GetPageResultAsync<WorkLogEntity>(db, input, dbSql.ToString(), parameters.ToArray());
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<WorkLogEntity> GetInfo(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<WorkLogEntity>(m => m.Id == id);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task Delete(WorkLogEntity entity)
        {
            using (var db = this.Basic)
            {
                await db.BeginTransactionAsync();
                await db.DeleteAsync(entity);
                await db.DeleteAsync<WorkLogShareEntity>(m => m.WorkLogId == entity.Id);
                await db.CommitTransactionAsync();
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="workLogShareList">共享人实体</param>
        /// <returns></returns>
        public async Task Create(WorkLogEntity entity, List<WorkLogShareEntity> workLogShareList)
        {
            using (var db = this.Basic)
            {
                await db.BeginTransactionAsync();
                await db.InsertAsync(entity);
                await db.InsertAsync(workLogShareList);
                await db.CommitTransactionAsync();
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="workLogShareList">共享人实体</param>
        /// <returns></returns>
        public async Task Update(WorkLogEntity entity, List<WorkLogShareEntity> workLogShareList)
        {
            using (var db = this.Basic)
            {
                await db.BeginTransactionAsync();
                await db.UpdateAsync(entity);
                await db.DeleteAsync<WorkLogShareEntity>(m => m.WorkLogId == entity.Id);
                await db.InsertAsync(workLogShareList);
                await db.CommitTransactionAsync();
            }
        }
    }
}